﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace Common
{
    /// <summary>
    /// Drawer that draws line graph from the data.
    /// </summary>
    public class LineDrawer : IStatisticsDrawer
    {
        Pen _dottedPen = new Pen(Color.Aquamarine) { DashPattern = new float[] { 0.5f, 1.0f } };

        /// <summary>
        /// Draw the data on the screen.
        /// </summary>
        /// <param name="g">The drawing context.</param>
        /// <param name="items">The data to display.</param>
        public void DrawData(DrawContext g, List<DataRow> items)
        {
            DrawGrid(g);
            foreach (var item in items)
            {
                DrawSingleGraph(g, item);
            }
        }

        private void DrawGrid(DrawContext g)
        {
            int stepSize = g.ToPixels(5.0);
            int currentY = stepSize;

            while (currentY < g.Height)
            {
                g.DrawLine(_dottedPen, new Point(0, currentY), new Point(g.Width, currentY));
                currentY += stepSize;
            }
        }

        private void DrawSingleGraph(DrawContext g, DataRow item)
        {
            const int stepX = 3;
            int x = g.Width;
            for (int i = 0; i < item.Data.Count - 1 && x > 0; ++i)
            {
                int currentY = g.ToPixels(item.Data[i]);
                int nextY = g.ToPixels(item.Data[i + 1]);
                g.DrawLine(item.Pen, new Point(x, currentY), new Point(x - stepX, nextY));
                x -= stepX;
            }
        }
    }
}
